package com.feng.util;
import java.text.DecimalFormat;

public class ZHTNumberFormatter {
	private static String pointPostfixNum[]={
			"圆","角","分","厘"
	};
	
	private static String number[]={
			"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"
	};
	
	private static String bigger[]={
			"",
			"萬",
			"亿",
			"兆",
	};
	private static String pos[]={
			"",
			"拾",
			"佰",
			"仟"
	};

	public static String convertCurrency(double num){
		DecimalFormat format=new DecimalFormat("####,###0.00");
		String result="";
		String temp=format.format(num);
		String topParts[]=temp.split("\\.");
		String pointPostfix=topParts[1];
		String parts[]=(topParts[0]).split("\\,");

		int k=0;
		for(int i=parts.length;i>0;i--){
			if(Integer.parseInt(parts[i-1])>0)
				result=subConvert(parts[i-1])+bigger[k]+result;
			else
				result="零"+result;
			k++;
		}
		String newString=""+result.charAt(0);
		for(int i=1;i<result.length();i++){
			if(result.charAt(i)=='零'&&result.charAt(i-1)=='零')
				continue;
			newString+=result.charAt(i);
		}
		if(newString.charAt(newString.length()-1)=='零')
			newString=newString.substring(0,newString.length()-1);
		
		newString=newString+"圆";
		temp="";
		if(Integer.parseInt(pointPostfix)!=0){
			if(pointPostfix.charAt(1)!='0')
				temp=number[pointPostfix.charAt(1)-'0']+"分";
			if(pointPostfix.charAt(0)!='0')
				temp=number[pointPostfix.charAt(0)-'0']+"角"+temp;
			else if(!temp.equals(""))
				temp="零"+temp;
			newString=newString+temp;
		}else{
			newString=newString+"整";
		}
		return "人民币"+newString;		
	}
	private static String subConvert(String part){
		String result="";
		int posIndex=0;
		for(int max=Math.min(4,part.length());
			max>0;
			max--)
		{
			if(part.charAt(max-1)!='0')
				result=number[part.charAt(max-1)-'0']+pos[posIndex]+result;
			else
				result="零"+result;
			posIndex++;
		}
		String newString=""+result.charAt(0);
		for(int i=1;i<result.length();i++){
			if(result.charAt(i)=='零'&&result.charAt(i-1)=='零')
				continue;
			newString+=result.charAt(i);
		}
		if(newString.charAt(newString.length()-1)=='零')
			newString=newString.substring(0,newString.length()-1);		
		return newString;
		
	}

	public static void main(String args[]){
		System.out.println(convertCurrency(100100111000.0110001d));
	}
}
